/**
 * 
 */
package table;

import helper.TableHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
/**
 * @author Ken
 *
 */
public class TableModel extends AbstractTableModel implements TableModelListener{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected String _tableName;
    protected JTable _table;
    private List<ColumnData> m_columns;
    protected Vector<List<String>> m_vector;
    private TableHelper _helper;
                             
    public TableModel() {
        super();
        m_columns = new ArrayList<ColumnData>();
        m_vector = new Vector<List<String>>();        
    }
    
    public void setColumnName(int index, String name) {    	    	   
        m_columns.get(index).m_title = name;        
    }

    protected TableModel(String tableName) {
        this.m_vector = new Vector<List<String>>();        
        this._tableName = tableName;
    }
    
    public TableModel(String table, List<ColumnData> m_columns) {
        super();
        this._tableName = table;
        this.m_columns = m_columns;
        m_vector = new Vector<List<String>>();
    }
               
    public TableModel(String table, List<ColumnData> m_columns, Vector<List<String>> m_vector) {
        super();
        this._tableName = table;
        this.m_columns = m_columns;
        this.m_vector = m_vector;
    }
    
    public void SetColumnData(List<ColumnData> columns){
        this.m_columns = columns;
    }
    
    @Override
    public int getRowCount() {
        return m_vector == null ? 0 : m_vector.size();
    }

    @Override
    public int getColumnCount() {
        return m_columns == null ? 0 : m_columns.size();
    }
            
    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (rowIndex < 0 || rowIndex >= getRowCount())
            return;
        List<String> row = getRow(rowIndex);
        if (row == null)
            return;
        if (row.get(columnIndex).toString().equals(aValue.toString()))
        	return;
        _helper.tableChangeValue(this);
        row.set(columnIndex, aValue.toString());                              
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rowIndex < 0 || rowIndex > getRowCount())
            return null;
        List<String> o = m_vector == null ? null : m_vector.elementAt(rowIndex);
        return o.get(columnIndex);                
    }
    
    @Override
    public String getColumnName(int column) {
        return m_columns == null ? "Unknown" : m_columns.get(column).m_title;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }
       
    public void removeRow(int index) {
        if (index < m_vector.size())
        {
            m_vector.remove(index);
            TableModelListener[] listeners = getTableModelListeners();
            for (int i = 0; i < listeners.length; i++) {
                listeners[i].tableChanged(new TableModelEvent(this, index, index, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
            }
        }
    }        
            
    public void bindModelToTable(JTable table){
        _table = table;
        table.setModel(this);
        RenderColumn();
    }
    
    protected void RenderColumn(){        
        for (int k = 0; k < this.getColumnCount(); k++) {
            DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
            renderer.setHorizontalAlignment(JLabel.CENTER);
            TableColumn column = new TableColumn(k, m_columns.get(k).m_width, renderer, null);
            _table.addColumn(column);            
            column.setIdentifier(m_columns.get(k).m_title);            
        }        
    }
    
    public void addRow(List<String> row){
        this.m_vector.addElement(row);
    }
    
    public void addRow(List<String> row, int index) {
        this.m_vector.add(index, row);        
    }
    
    public List<String> getRow(int index){
        if (index < 0 || index >= getRowCount())
            return null;
        return (List<String>) m_vector.get(index);
    }

	@Override
	public void tableChanged(TableModelEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @return the _helper
	 */
	public TableHelper get_helper() {
		return _helper;
	}

	/**
	 * @param _helper the _helper to set
	 */
	public void set_helper(TableHelper _helper) {
		this._helper = _helper;
	}

	/**
	 * @return the m_columns
	 */
	public List<ColumnData> getM_columns() {
		return m_columns;
	}

	/**
	 * @param m_columns the m_columns to set
	 */
	public void setM_columns(List<ColumnData> m_columns) {
		this.m_columns = m_columns;
	}
}